/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.scriptprogram.wizard;

import net.zarubsys.robotplayground.gui.session.utils.ExistingFileSelectionPage;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

/**
 * ScriptProgramWizard
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class ScriptProgramWizard extends Wizard {
	
	private ScriptProgramSourceSelectionPage sourceSelectionPage;
	private WizardNewFileCreationPage newScriptPage;
	private ExistingFileSelectionPage existingScriptPage;
	
	private IFile resultFile;

	public ScriptProgramWizard() {
		super();
		setWindowTitle("New Script Program");
	}
	
	@Override
	public void addPages() {
		sourceSelectionPage = new ScriptProgramSourceSelectionPage("Script source");
		sourceSelectionPage.setDescription("Please select script source");
		addPage(sourceSelectionPage);
		
		newScriptPage = new WizardNewFileCreationPage("fileSelect", StructuredSelection.EMPTY);
		newScriptPage.setTitle("Select destination");
		newScriptPage.setDescription("Select destination where new script will be located.");
		newScriptPage.setFileExtension("script");
		addPage(newScriptPage);
		
		existingScriptPage = new ExistingFileSelectionPage("Select script", "Script", "script");
		existingScriptPage.setDescription("Please select exiting script using browse button");
		addPage(existingScriptPage);
	}
	
	@Override
	public IWizardPage getNextPage(IWizardPage page) {
		if (page == sourceSelectionPage) {
			if (sourceSelectionPage.getSource() == ScriptProgramSourceSelectionPage.ProgramSourceSelection.NEW) {
				return newScriptPage;
			}
			return existingScriptPage;
		} else if (page == newScriptPage) {
			return null;
		} else if (page == existingScriptPage) {
			return null;
		} else {
			throw new RuntimeException("Unknown page state");
		}
	}

	@Override
	public boolean performFinish() {
		if (sourceSelectionPage.getSource() == ScriptProgramSourceSelectionPage.ProgramSourceSelection.NEW) {
			resultFile = newScriptPage.createNewFile();
			return true;
		}
		resultFile = existingScriptPage.getResult();
		return true;
	}
	
	@Override
	public boolean canFinish() {
		return newScriptPage.isPageComplete() || existingScriptPage.isPageComplete();
	}
	
	public IFile getResultFile() {
		return resultFile;
	}

}

